/*
  FileName : USB_DeviceApp.h

  Desctiption :

  Created on 2007.9.19 by ZZP.

  Revision History:

    
*/


#ifndef __USB_DEVICE_APP_H__
#define __USB_DEVICE_APP_H__

#include "Usb_Descriptor.h"

#define FULL_SPEED 0
#define HIGH_SPEED 1

#define DC_DEFAULT_STATE   0
#define DC_ADDRESS_STATE   1
#define DC_CONFIG_STATE    2

#define SETUP_STATE_NONE   0
#define SETUP_STATE_IN     1
#define SETUP_STATE_OUT    2
#define SETUP_STATE_STATUS 3

//Data to be handled state:
#define DATA_STATE_IDLE     0
#define DATA_STATE_SETUP    1
#define DATA_STATE_EP0RX    2
#define DATA_STATE_EP0TX    3
#define DATA_STATE_CBW      4
#define DATA_STATE_DATA_OUT 5
#define DATA_STATE_DATA_IN  6
#define DATA_STATE_CSW      7

#define BULK_DATA_OUT       0
#define BULK_DATA_IN        1
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
//                  Device controller define
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
//8-6-80 Device : 
#define MASS_STORAGE_CLASS    0x08
#define SCSI_SUBCLASS         0x06
#define BULK_ONLY_PROTOCOL    0x50

#define BulkMaxPacketSize      0x40  //64 bytes for bulk/interrupt endpoint.
#define CtrlMaxPacketSize      0x20
//DC ID
#define VENDER_ID          0x0000
#define PRODUCT_ID         0x0000
#define DEVICE_RELEASE     0x0100
/*
//string index
#define STR_INDEX_LANGUAGE						    0x00	
#define STR_INDEX_MANUFACTURER					    0x01	
#define STR_INDEX_PRODUCT						    0x02	
#define STR_INDEX_SERIALNUMBER					    0x03
#define STR_INDEX_CONFIGURATION					    0x04	
#define STR_INDEX_INTERFACE						    0x05

//Descriptor index
#define DEVICE_DESCRIPTOR               0x01
#define CONFIG_DESCRIPTOR               0x02
#define STRING_DESCRIPTOR               0x03
#define INTERFACE_DESCRIPTOR            0x04
#define ENDPOINT_DESCRIPTOR             0x05
#define DEVICE_QUALIFIER_DESCRIPTOR     0x06
#define OTHER_SPEED_CONFIG_DESCRIPTOR   0x07
#define INTERFACE_POWER_DESCRIPTOR      0x08

//Standard request index
#define GET_STATUS              0x00
#define CLEAR_FEATURE           0x01
#define SET_FEATURE             0x03
#define SET_ADDRESS             0x05
#define GET_DESCRIPTOR          0x06
#define SET_DESCRIPTOR          0x07
#define GET_CONFIG              0x08
#define SET_CONFIG              0x09
#define GET_INTERFACE           0x0A
#define SET_INTERFACE           0x0B
#define SYNCH_FRAME             0x0C

//Feature index
#define ENDPOINT_HALT           0x00
#define DEVICE_REMOTE_WAKEUP    0x01
#define TEST_MODE               0x02


//Endpoint
#define Number_EndPoint         0x02
#define MAX_ENDPOINTS           0x0F

#define ENDPOINT_TYPE_CONTROL      0x00
#define ENDPOINT_TYPE_ISOCHRONOUS  0x01
#define ENDPOINT_TYPE_BULK         0x02
#define ENDPOINT_TYPE_INTERRUPT    0x03

//EP1 : Bulk_Out   EP2 : Bulk_In
#define ENDPOINT_1_OUT          0x01
#define ENDPOINT_2_IN           0x82

#define ENDPSTS_HALT            0x01

#define USB_ENDPOINT_DIRECTION_MASK               0x80
#define USB_ENDPOINT_TYPE_MASK                    0x03

#define USB_FEATURE_ENDPOINT_STALL          0x0000
#define USB_FEATURE_REMOTE_WAKEUP           0x0001
#define USB_FEATURE_POWER_D0                0x0002
#define USB_FEATURE_POWER_D1                0x0003
#define USB_FEATURE_POWER_D2                0x0004
#define USB_FEATURE_POWER_D3                0x0005

// FSM for Default Control Pipe
// One-Hot dinfition4DCPFSM
#define USBFSM4DCP_IDLE             0x00
#define USBFSM4DCP_SETUPPROC        0x01
#define USBFSM4DCP_DATAIN           0x02
#define USBFSM4DCP_DATAOUT          0x04
#define USBFSM4DCP_HANDSHAKE4CO     0x08
#define USBFSM4DCP_HANDSHAKE4CI     0x10
#define USBFSM4DCP_STALL            0x80
#define USBFSM4DCP_BUSRESET         0x20

// FSM for Bulk-Only Transfer
// One-Hot dinfition4BOTFSM
#define USBFSM4BOT_STALL            0x80
#define USBFSM4BOT_IDLE             0x01
#define USBFSM4BOT_CBWPROC          0x02
#define USBFSM4BOT_DATAIN           0x04
#define USBFSM4BOT_DATAOUT          0x08
#define USBFSM4BOT_CSWPROC          0x10
#define USBFSM4BOT_CSW              0x20


#define USB_RECIPIENT            0x1F
#define USB_RECIPIENT_DEVICE     0x00
#define USB_RECIPIENT_INTERFACE  0x01
#define USB_RECIPIENT_ENDPOINT   0x02

#define USB_REQUEST_TYPE_MASK    0x60
#define USB_STANDARD_REQUEST     0x00
#define USB_CLASS_REQUEST        0x20
#define USB_VENDOR_REQUEST       0x40

#define USB_REQUEST_MASK         0xFF

#define DEVICE_ADDRESS_MASK      0x7F

#define DEVSTS_SELFPOWERED       0x01
#define DEVSTS_REMOTEWAKEUP      0x02


#define MAX_STD_REQUEST     12
#define MAX_CLASS_REQUEST   2
#define MAX_VENDOR_REQUEST  1*/

/*
typedef struct _DEVICE_DESCRIPTOR_STRUCT
{
  unsigned char bLength;
  unsigned char bDescriptorType;
  unsigned int  bcdUSB;
  unsigned char bDeviceClass;
  unsigned char bDeviceSubClass;
  unsigned char bDeviceProtocol;
  unsigned char bMaxPacketSize0;
  unsigned int  idVendor;
  unsigned int  idProduct;
  unsigned int  bcdDevice;
  unsigned char iManufacture;
  unsigned char iProduct;
  unsigned char iSerialNumber;
  unsigned char bNumConfigs;
}
DEVICE_DESCRIPTOR_STRUCT,*pDEVICE_DESCRIPTOR_STRUCT;

typedef struct _COFIGURATION_DESCRIPTOR_STRUCT
{
  unsigned char bLength;
  unsigned char bDescriptorType;
  unsigned int  wTotalLength;
  unsigned char bNumInterfaces;
  unsigned char bConfigValue;
  unsigned char iConfig;
  unsigned char bmAttributes;
  unsigned char MaxPower;
}
CONFIG_DESCRIPTOR_STRUCT,*pCONFIG_DESCRIPTOR_STRUCT;

typedef struct _INTERFACE_DESCRIPTOR_STRUCT
{
  unsigned char bLength;
  unsigned char bDescriptorType;
  unsigned char bInterfaceNumber;
  unsigned char bAlterlateSetting;
  unsigned char bNumEndPoints;
  unsigned char bInterfaceClass;
  unsigned char bInterfaceSubClass;
  unsigned char bInterfaceProtocol;
  unsigned char iInterface;
}
INTERFACE_DESCRIPTOR_STRUCT,*pINTERFACE_DESCRIPTOR_STRUCT;

typedef struct _ENDPOINT_DESCRIPTOR_STRUCT
{
  unsigned char bLength;
  unsigned char bDescriptorType;
  unsigned char bEndPointAddress;
  unsigned char bmAttributes;
  unsigned int  wMaxPacketSize;
  unsigned char bInterval;
}
ENDPOINT_DESCRIPTOR_STRUCT,*pENDPOINT_DESCRIPTOR_STRUCT;



typedef struct _USB_STRING_LANGUAGE_DESCRIPTOR {
	unsigned int DescType_Length;  //bLength;
	                               //INT8  bDescriptorType;
	unsigned int ulanguageID;
} USB_STRING_LANGUAGE_DESCRIPTOR,*PUSB_STRING_LANGUAGE_DESCRIPTOR;

typedef struct _USB_STRING_INTERFACE_DESCRIPTOR {
	unsigned int DescType_Length;  //bLength;
	                               //INT8  bDescriptorType;
	unsigned int Interface[11];
} USB_STRING_INTERFACE_DESCRIPTOR,*PUSB_STRING_INTERFACE_DESCRIPTOR;

typedef struct _USB_STRING_CONFIGURATION_DESCRIPTOR {
	unsigned int DescType_Length;  //bLength;
	                               //INT8  bDescriptorType;
	unsigned int Configuration[8];
} USB_STRING_CONFIGURATION_DESCRIPTOR,*PUSB_STRING_CONFIGURATION_DESCRIPTOR;

typedef struct _USB_STRING_SERIALNUMBER_DESCRIPTOR {
	unsigned int DescType_Length;  //bLength;
	                               //INT8  bDescriptorType;
	unsigned int SerialNum[12];
} USB_STRING_SERIALNUMBER_DESCRIPTOR,*PUSB_STRING_SERIALNUMBER_DESCRIPTOR;

typedef struct _USB_STRING_PRODUCT_DESCRIPTOR {
	unsigned int DescType_Length;  //bLength;
	                               //INT8  bDescriptorType;
	unsigned int Product[15];
} USB_STRING_PRODUCT_DESCRIPTOR,*PUSB_STRING_PRODUCT_DESCRIPTOR;

typedef struct _USB_STRING_MANUFACTURER_DESCRIPTOR {
	unsigned int DescType_Length;  //bLength;
	                               //INT8  bDescriptorType;
	unsigned int Manufacturer[12];
} USB_STRING_MANUFACTURER_DESCRIPTOR,*PUSB_STRING_MANUFACTURER_DESCRIPTOR;



typedef struct _STRING_DESCRIPTOR_STRUCT
{
  unsigned char bLength;
  unsigned char bDescriptorType;
  unsigned char SomeDescriptor[36];
}
STRING_DESCRIPTOR_STRUCT,*pSTRING_DESCRIPTOR_STRUCT;

typedef struct _REQUEST_CMD_STRUCT
{
  unsigned int bmReqTypeReq;
  unsigned int wValue;
  unsigned int wIndex;
  unsigned int wLength;
}
REQUEST_CMD_STRUCT,*pREQUEST_CMD_STRUCT;
*/
struct SETUP_REQUEST_BITS{
  Uns ReqType : 8;
  Uns Request : 8;
};
union SETUP_REQUEST_DATA{
  Uns all;
  struct SETUP_REQUEST_BITS bit;
};
struct SETUP_wVALUE_BITS{
  Uns LO  : 8;
  Uns Hi  : 8;
};
union SETUP_wVALUE_DATA{
  Uns all;
  struct SETUP_wVALUE_BITS bit;
};

struct SETUP_COMMAND_DATA{
  union SETUP_REQUEST_DATA ReqBits;
  union SETUP_wVALUE_DATA wValueBits;
  Uns wIndex;
  Uns wLength;
};

struct DC_INFO{
  Uns Speed;
  Uns State;
  Uns Addr;
  Uns CfgValue;
  struct SETUP_COMMAND_DATA Command;
  Uns SetupState;
  Uns CtrlRxBuf[16];
  Uns CtrlRxBytes;
  Uns CtrlRxComplete;
  Uns CtrlTxBuf[16];
  Uns CtrlTxBytes;
  Uns CtrlTxComplete;
  Uns BulkState;
  Uns BulkMaxPktSz;
  LgUns BulkRxBytes;
  LgUns BulkTxBytes;
  Uns BulkCSWComplete;
  Uns BulkDataIntF;
  Uns BulkDataOutF;
  CBW DcCBW;
  CSW DcCSW;
  volatile Uns ExitF;
  Uns DataDir;
  Uns PhaseErr;
  Uns *pData;
  Uns FirstDataF;
  LgUns dwResidue;

  Uns DcRamPtr;

  struct DEVICE_DESCRIPTOR_STRUCT DevDesc;
  struct DEVICE_QUALIFIER_STRUCT DevQlfDesc;
  struct CONFIG_DESCRIPTOR_STRUCT CfgDesc;
  struct INTERFACE_DESCRIPTOR_STRUCT IntfDesc;
  struct ENDPOINT_DESCRIPTOR_STRUCT EPDescEP1RX;
  struct ENDPOINT_DESCRIPTOR_STRUCT EPDescEP1TX;
  Uns CfgDescTotal[16];
};

extern Uns BOTXferBuf[65536];
extern volatile struct DC_INFO DcInfo;

extern Uns DcRxBuf[4096];

extern volatile Uns DcTm;

extern volatile Uns TestRun;

extern void Dc_Interrupt_Isr(void);

extern void RxData(void);
extern void TxData(void);


#endif

//--------- End of File --------------



